import {} from "../api/index"
import {getGroupMessages} from "./db"

// 聊天
export const send = () => {}

// 重新编辑问题 | 重新生成回答
export const reSend = () => {}

// 喜欢
export const like = () => {}

// 不喜欢 反馈
export const comment = () => {}

// 复制
export const copy = () => {}

// 删除分组
export const delGroup = () => {}

// 重设分组标题
export const resetGroupTitle = () => {}

// 创建一个新的分组 开始一个新的对话
export const newGroup = () => {}

// 获取推荐问题集合
export const getSubjects = () => {}

// 上传文件
export const uploadFile = () => {}

// 时间间隔
const D = 1000 * 60 * 60 * 24;
export const getTimeBefore = (s) => {
    const t = new Date().getTime();
    const d = t - s;
    console.log('.........',s,d,D)
    return Math.floor(d / D)
}

// 获取历史 message 需要指定 groupID
export const getHistoryMessages = async (gid) => {
    const ms = await getGroupMessages(gid);
    const res = {root:[]}
    // 分组
    for(const m of ms){
        const {parent_id} = m
        let k = parent_id
        if(k == null){
            k = "root";
        }
        else if(res[k] == undefined){
            res[k] = [] 
        }
        res[k].push(m)
    }
    return res;
}


// 存缓存
const PREFIX_AI_CHAT = 'AI-CHAT-'
//
export const saveToStore = (name, value) => {
    const data = { value, type: typeof value, time: new Date().getTime() };
    localStorage.setItem(`${PREFIX_AI_CHAT}${name}`, JSON.stringify(data))
}

export const getFromStore = (name, dv) => {
    const t = localStorage.getItem(`${PREFIX_AI_CHAT}${name}`);
    if (t) {
        const data = JSON.parse(t);
        return data.value;
    }
    return dv || undefined;
}

// 获取当前的 groupId
const GROUP_ID = "GROUP_ID"
export const getCurrentGroupId = () => {
    return getFromStore(GROUP_ID)
}
// 设置当前 groupId
export const setCurrentGroupId = (id) => {
    saveToStore(GROUP_ID,id)
}
